/**
  ******************************************************************************
  * File Name          : dataprocess.c
  * Description        : Code for data process applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "syscfg.h"
#include "collector.h"
#include "lora.h"
#include "tskcfg.h"
#include "dwlcd.h"
#include "ctrl_common.h"

#define COLLECTOR_DATA_FLAG_POWR       0x0001
#define COLLECTOR_DATA_FLAG_TEMP       0x0002
#define COLLECTOR_DATA_FLAG_HUMI       0x0004
#define COLLECTOR_DATA_FLAG_NH3        0x0008
#define COLLECTOR_DATA_FLAG_CO2        0x0010
#define COLLECTOR_DATA_FLAG_TVOC       0x0020
#define COLLECTOR_DATA_FLAG_PRES       0x0040
#define COLLECTOR_DATA_FLAG_LUMI       0x0080
#define COLLECTOR_DATA_FLAG_H2S        0x0100

volatile Collector_LocalDataType Collector_LocalData;

/* lora task */
TaskHandle_t xAdcNtc_Thd = NULL;
StaticTask_t xAdcNtc_Tcb;
StackType_t  xAdcNtc_Stk[ ADCNTC_STACK_SIZE ];

QueueHandle_t NTC_TrsMsg_Qhd = NULL;
static StaticQueue_t ntc_trsmsg_stru;
static uint8_t       ntc_trsmsg_area[NTC_TRSMSG_QUE_LENGTH * NTC_TRSMSG_QUE_ITEMSIZE];

#if(COLLECTOR_USE_TYPE == COLLECTOR_USE_LORA)
TaskHandle_t xCollertor_Thd = NULL;
StaticTask_t xCollertor_Tcb;
StackType_t  xCollertor_Stk[COLLERTOR_STACK_SIZE];

const static uint8_t readreg[1] = {0x5Au};

static void collector_lora_deal(void)
{
	volatile uint8_t i, temp_count = 0, hum_count = 0,  nh3_count = 0;
  volatile uint8_t co2_count = 0,  tvoc_count = 0, pres_count = 0;
	volatile int32_t  temp = 0;
	volatile uint32_t hum = 0;
	volatile uint32_t nh3 = 0;
  volatile uint32_t co2 = 0;
	volatile uint32_t tvoc = 0;
	volatile uint32_t pres = 0;
	
	for(i=0; i<SysCfg_RunPara.lora_num; i++)
	{
		if(Collector_LocalData.update[i] == 1)
		{
			if((Collector_LocalData.device[i].flag & COLLECTOR_DATA_FLAG_TEMP) != 0x0000)
			{
				temp_count++;
				temp += Collector_LocalData.device[i].temperature;
			}
			
			if((Collector_LocalData.device[i].flag & COLLECTOR_DATA_FLAG_HUMI) != 0x0000)
			{
				hum_count++;
				hum += Collector_LocalData.device[i].humidity;
			}
			
			if((Collector_LocalData.device[i].flag & COLLECTOR_DATA_FLAG_NH3) != 0x0000)
			{
				nh3_count++;
				nh3 += Collector_LocalData.device[i].nh3;
			}
      
      if((Collector_LocalData.device[i].flag & COLLECTOR_DATA_FLAG_CO2) != 0x0000)
			{
				co2_count++;
				co2 += Collector_LocalData.device[i].co2;
			}
			
			if((Collector_LocalData.device[i].flag & COLLECTOR_DATA_FLAG_TVOC) != 0x0000)
			{
				tvoc_count++;
				tvoc += Collector_LocalData.device[i].tvoc;
			}
			
			if((Collector_LocalData.device[i].flag & COLLECTOR_DATA_FLAG_PRES) != 0x0000)
			{
				pres_count++;
				pres += Collector_LocalData.device[i].pressure;
			}
		}
		
		Collector_LocalData.update[i] = 0;  /* 数据用完后,将更新标志位清零 */
	}
	
	if(temp_count != 0)
	{
		Collector_LocalData.average.temperature = (temp/temp_count);
	}
	if(hum_count != 0)
	{
		Collector_LocalData.average.humidity = (hum/hum_count);
	}
	if(nh3_count != 0)
	{
		Collector_LocalData.average.nh3 = (nh3/nh3_count);
	}
  if(co2_count != 0)
	{
		Collector_LocalData.average.co2 = (co2/co2_count);
	}
	if(tvoc_count != 0)
	{
		Collector_LocalData.average.tvoc = (tvoc/tvoc_count);
	}
	if(pres_count != 0)
	{
		Collector_LocalData.average.pressure = (pres/pres_count);
	}
	
	DW_Send_temp();
	DW_Send_nodetemp();
}

uint8_t LoRa_LinkRecvCallback(uint8_t srcid, uint8_t *pl, uint8_t len)
{
	if(srcid > COLLECTOR_LORADEV_MAXNUM)
	{
		return 1;
	}
  
	if(len != 0x0C)
	{
		return 2;
	}	
	
  Collector_LocalData.device[srcid-1] = *((Collector_DataType *)pl);
	
	SysUsrLog("D:%d,T:%d,H:%d,N:%d\r\n", srcid, 
	                                     Collector_LocalData.device[srcid-1].temperature, 
	                                     Collector_LocalData.device[srcid-1].humidity, 
	                                     Collector_LocalData.device[srcid-1].nh3);
	return 0;
}

void vCollertor_Task( void * pvParameters )
{
	uint8_t i;

	LoRa_Init();

	while(1)
	{
		for(i=0; i<SysCfg_RunPara.lora_num; i++)
    {
      if(LoRa_LinkLoopback(i+1, readreg, 1) == 0)
      {
        LED_LORA_COUNT(LED_ON);
        
        Collector_LocalData.update[i] = 1;
        
        vTaskDelay(133);
        
        LED_LORA_COUNT(LED_OFF);
      }
      else
      {
        Collector_LocalData.update[i] = 0;
      }
    }
		
    collector_lora_deal();
	}
}
#endif

void vAdcNtc_Task( void * pvParameters )
{
	TickType_t tick;
	uint8_t pow_flag = 0;
	
#if(COLLECTOR_USE_TYPE == COLLECTOR_USE_NTC)
	uint8_t i, ntc_err_typ = 0, ntc_err_cnt = 0;
	
	NTC_TrsMsg_Qhd = xQueueCreateStatic(NTC_TRSMSG_QUE_LENGTH, NTC_TRSMSG_QUE_ITEMSIZE,
	                                    ntc_trsmsg_area, &ntc_trsmsg_stru);
#endif

	NTC_Config();
	
	tick = xTaskGetTickCount();
	
	while(1)
	{
#if(COLLECTOR_USE_TYPE == COLLECTOR_USE_NTC)
		vTaskDelayUntil(&tick, 3333);
		
    NtcPow_GetValue(&pow_flag, &ntc_err_typ, &ntc_err_cnt);
		
		DW_Send_temp();
		DW_Send_nodetemp();

		for(i=0; (i<ntc_err_typ)&&(i<4); i++)
		{
			LED_NEC_ERR(LED_ON);
			vTaskDelay(250);
			LED_NEC_ERR(LED_OFF);
			vTaskDelay(250);
		}
		LED_NEC_ERR(LED_OFF);
		
		if(ntc_err_cnt > 5)
		{
			ntc_err_cnt = 6;
			Ctrl_BjTrigger = 1;
			/* alarm */
		}
		else
		{
			Ctrl_BjTrigger = 0;
		}
		
		if(pow_flag > 4)
		{
			pow_flag = 0;
			Route_gw_tonet_poweroff();
		}
		
#else
		vTaskDelayUntil(&tick, 6000);
		
		Pow_GetValue(&pow_flag);

		if(pow_flag > 2)
		{
			pow_flag = 0;
			Route_gw_tonet_poweroff();
		}
#endif
	}
}
